
import requests
import json
import datetime
import utils
import config


def get_baidu_limit_data(date,stock_code):
    #1、取数
    #设置请求的头
    #请求百度获取数据
    #返回标准json
    headers = {
            'User-agent': "Mozilla 5.10",
            'cache-control': "no-cache",
            'postman-token': "220d2989-c111-fea3-874f-f5c31113db59"
    }

    url_stock=config.getConfig()["url"]["stock_baidu_limit_up"]
    url = url_stock.replace("stockcode", stock_code)
    print(url)
    print(headers)
    res = requests.get(url,headers=headers)
    result_json = json.loads(res.text)
    print(result_json)
    price = result_json["Result"]["cur"]["price"]
    limitUp = result_json["Result"]["pankouinfos"]["list"][15]["value"]
    limitDown = result_json["Result"]["pankouinfos"]["list"][18]["value"]
    avgPrice = result_json["Result"]["pankouinfos"]["list"][13]["value"]
    print(price)
    print(limitUp)
    print(limitDown)
    if price ==  limitUp:
        print("涨停了")
        type = "涨停"
        bidprice0 = result_json["Result"]["buyinfos"][0]["bidvolume"]
        bidprice1 = result_json["Result"]["buyinfos"][1]["bidvolume"]
        bidprice2 = result_json["Result"]["buyinfos"][2]["bidvolume"]
        bidprice3 = result_json["Result"]["buyinfos"][3]["bidvolume"]
        bidprice4 = result_json["Result"]["buyinfos"][4]["bidvolume"]
        utils.save_stock_limit_data(date,stock_code,price,avgPrice,type,bidprice0,bidprice1,bidprice2,bidprice3,bidprice4)
    if price == limitDown:
        print("跌停了")
        type = "跌停"
        bidprice0 = result_json["Result"]["askinfos"][4]["askvolume"]
        bidprice1 = result_json["Result"]["askinfos"][3]["askvolume"]
        bidprice2 = result_json["Result"]["askinfos"][2]["askvolume"]
        bidprice3 = result_json["Result"]["askinfos"][1]["askvolume"]
        bidprice4 = result_json["Result"]["askinfos"][0]["askvolume"]
        utils.save_stock_limit_data(date,stock_code,price,avgPrice,type,bidprice0,bidprice1,bidprice2,bidprice3,bidprice4)
    return 0



    #
    # print(res)
    # result_text= json.loads(res.text)
    # print(result_text)
    # result_list = result_text["data"]["plate_list"]
    # print(result_list)
    # #2、存储到mysql
    # #utils.save_quote_txn(result_json['data']['diff'])
    # utils.clean_10jqka_concept()
    # for i in result_list:
    #     utils.save_10jqka_concept(i)
    # print( "热点概念数据已经入库，共计" + str(len(result_list)) +"条数据。")

get_baidu_limit_data("002332")